package com.liu.user.controller;

import com.liu.common.entity.Result;
import com.liu.common.status.CodeEnum;
import com.liu.common.utils.JwtUtil;
import com.liu.user.entity.Admin;
import com.liu.user.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/8/21 0021.
 */

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {


    @Autowired
   private AdminService adminService;

    @Autowired
    private JwtUtil jwtUtil;


    @RequestMapping(method = RequestMethod.POST)
    public Result addAdmin(@RequestBody  Admin admin){


        adminService.addAdmin(admin);


        return  new Result(true, CodeEnum.OK.getCode(),"管理员添加成功");
    }


    @PostMapping("/login")
    public Result login(@RequestBody  Admin admin){


      Admin adminlogin =  adminService.login(admin.getLoginName(),admin.getPassword());
      if(adminlogin!=null){
          String token= jwtUtil.createJWT(adminlogin.getId(),adminlogin.getLoginName(),"admin");

          Map map = new HashMap();
          map.put("roles","admin");
          map.put("token",token);


          return  new Result(true, CodeEnum.OK.getCode(),"用户登录成功",map);
      }
        return  new Result(false, CodeEnum.LOGIN_ERROR.getCode(),CodeEnum.LOGIN_ERROR.getMessage());

    }





}
